#include "modbusshow.h"
#include "ui_modbusshow.h"

ModbusShow::ModbusShow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusShow)
{
    ui->setupUi(this);
//    ui->widget->setStyleSheet("QWidget {""  border: 6px solid cyan;"
//"  border-radius: 13px;""}");
//    ui->widget->setGeometry(20, 100, 671, 121);
//    ui->widget_2->setStyleSheet("QWidget {""  border: 6px solid cyan;"
//"  border-radius: 13px;""}");
//    ui->widget_2->setGeometry(20, 270, 671, 121);
//    ui->widget_3->setStyleSheet("QWidget {""  border: 6px solid cyan;"
//"  border-radius: 13px;""}");
//    ui->widget_3->setGeometry(20, 460, 671, 121);
    ui->pushButton_jichuang->setStyleSheet(QPushButton_famen_STYTLE_CLOSE);
    ui->pushButton_jichuang->setCheckable(true);
    ui->pushButton_jichuang->setText("阀门关");
    connect(ui->pushButton_jichuang,SIGNAL(clicked()),this,SLOT(pushButtonjichuang()));
    ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_CLOSE);
    ui->pushButton_Fan->setCheckable(true);
    ui->pushButton_Fan->setText("风机关");
    connect(ui->pushButton_Fan,SIGNAL(clicked()),this,SLOT(pushButtonFan()));
    connect(ui->pushButton_flash,SIGNAL(clicked()),this,SLOT(flashSlot()));
}

ModbusShow::~ModbusShow()
{
    delete ui;
}

void ModbusShow::dataUpSlot(const QByteArray &message)
{
    QString messageString = QString::fromUtf8(message);
    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(message,&jsonError);
    if (!jsonDoc.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        //qDebug() << "Received message:" << messageString;
        QJsonObject jsonObj = jsonDoc.object();

        // 访问顶层字段
        int type = jsonObj["type"].toInt();
        int result = jsonObj["result"].toInt();
        qDebug() <<"type:" <<type;
        qDebug()<<"result:" <<result;

        // 访问嵌套的JSON对象
        QJsonArray dataArray = jsonObj["data"].toArray();
           for (const QJsonValue &value : dataArray) {
               QJsonObject itemObj = value.toObject();
               int key = itemObj["key"].toInt();
               QString val = itemObj["val"].toString();
               switch (key) {
               case 101:
                  ui->waterconsumpution->setText(val);
                  break;
               case 102:
                  ui->etcnum->setText(val);
                  break;
               case 103:
                   ui->mecpower->setText(val);
                   break;
               case 104:
                   ui->mectem->setText(val);
                   break;
               case 105:
                   ui->mecnum->setText(val);
                   break;
               case 106:
                   ui->thingsnum->setText(val);
                   break;
               case 107:
                   ui->passrate->setText(val);
                   break;
               default:
                   break;
               }
               //qDebug() << "Key:" << key << ", Value:" << val;
           }
      }
}

void ModbusShow::pushButtonjichuang()
{
    if(ui->pushButton_jichuang->isChecked())
    {
        ui->pushButton_jichuang->setStyleSheet(QPushButton_famen_STYTLE_OPEN);
        ui->pushButton_jichuang->setText("阀门开");
        ui->pushButton_jichuang->node.data_type="1";
    }
    else if(!ui->pushButton_jichuang->isChecked())
    {
        ui->pushButton_jichuang->setStyleSheet(QPushButton_famen_STYTLE_CLOSE);
        ui->pushButton_jichuang->setText("阀门关");
        ui->pushButton_jichuang->node.data_type="0";
    }
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_jichuang->node.type=2;
    ui->pushButton_jichuang->node.key=109;
    node_data.insert("type",ui->pushButton_jichuang->node.type);
    node_data_data.insert("key",ui->pushButton_jichuang->node.key);
    node_data_data.insert("val",ui->pushButton_jichuang->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
        qDebug()<<QString(bytearray);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);

}

void ModbusShow::pushButtonFan()
{
    if(ui->pushButton_Fan->isChecked())
    {
        ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_OPEN);
        ui->pushButton_Fan->node.data_type="1";
        ui->pushButton_Fan->setText("风机开");
    }
    else if(!ui->pushButton_Fan->isChecked())
    {
        ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_CLOSE);
        ui->pushButton_Fan->node.data_type="0";
        ui->pushButton_Fan->setText("风机关");
    }
    ui->pushButton_Fan->node.key=108;
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_Fan->node.type=2;
    node_data.insert("type",ui->pushButton_Fan->node.type);
    node_data_data.insert("key",ui->pushButton_Fan->node.key);
    node_data_data.insert("val",ui->pushButton_Fan->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}

void ModbusShow::flashSlot()
{
    QJsonObject node_data;
    QJsonDocument document;
    ui->pushButton_flash->node.type=1;
    ui->pushButton_flash->node.limit="all";
    node_data.insert("type",ui->pushButton_flash->node.type);
    node_data.insert("limit",ui->pushButton_flash->node.limit);
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}
